<?php

/**
 * 后台文章模块 Model
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zhangwentao $  <zwttmac@qq.com>
 * $Id: article.class.php 1402 2011-03-03 10:15:34Z zhangwentao $
 * http://www.t-mac.org；
 */
class article extends Model
{

    private $class_table;
    private $article_table;
    private $city_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->connect();
        $this->class_table = '#@__article_class';
        $this->article_table = '#@__article';
        $this->city_table = '#@__city';
    }

    /**
     * 保存
     * @param string $menusMain
     * return Boole
     */
    public function addArticle($info, $mode = 'INSERT', $where = '')
    {
        $rs = $this->db->autoExecute($this->article_table, $info, $mode, $where);
        return $rs;
    }

    /**
     * 获取一个资讯栏目信息
     * @param int $class_id 栏目id
     * return array
     */
    public function getArticleInfo($aid)
    {
        $rs = $this->db->getRow("SELECT * FROM $this->article_table WHERE aid = $aid LIMIT 0,1");
        return $rs;
    }

    /**
     * 获取资讯栏目
     * return article_class,pages
     */
    public function getClass()
    {
        $rs = $this->db->cacheGetAll("SELECT * FROM $this->class_table ORDER BY orderid DESC, class_id DESC", 10);
        return $rs;
    }

    /**
     * 获取所有资讯
     * return article_class,pages
     */
    public function getArticleList($url = null)
    {
        $count = $this->db->getOne("SELECT COUNT(*) FROM $this->article_table");
        if ($url == '')
            $url = 'index.php?m=admin/article&page=';
        $pages = $this->P('Pages');
        $pages->setTotal($count);
        $pages->setUrl($url);
        $pages->setPrepage(15);
        $limit = $pages->getSqlLimit();
        $rs = $this->db->getAll("SELECT aid, class_id, title, content, state_radio, time FROM $this->article_table ORDER BY aid DESC LIMIT $limit");
        //取所有的资讯栏目 不用LEFT JOIN取class_name
        $rs_class = $this->getClass();
        $ary_class = array();
        foreach ($rs_class AS $kk => $vv) {
            $ary_class[$vv['class_id']] = $vv['class_name'];
        }
        //推荐状态数组
        $state_radio_ary = UtilityConfig::state_radio_ary();
        //遍历通过class_id取class_name
        foreach ($rs AS $k => $v) {
            $rs[$k]['class_name'] = $ary_class[$v['class_id']];
            $rs[$k]['state_name'] = $state_radio_ary[$v['state_radio']];
            $rs[$k]['time'] = date('Y/m/d H:i:s', $v['time']);
        }

        //把文章的当前page写到cookies里
        HttpResponse::setCookie('article_page', $pages->getNowPage());

        $ErrorMsg = '';
        if ($count == 0) {
            $ErrorMsg = "暂无资讯文章!   <a href='index.php?m=admin/article.add' class='link_a'>猛点我来添加新资讯</a>";
        }
        
        $this->assign('rs', $rs);
        $this->assign('pageCurrent', $pages->getNowPage());
        $this->assign('page', $pages->show());
        $this->assign('ErrorMsg', $ErrorMsg);
        return $rs;
    }

    /**
     * 取省列表 或根据省id取城市列表
     * @param <type> $type  默认c取city列表 p取province列表
     * @param <int>  $pid   省级id
     * @return array
     */
    public function getCity($type = 'c', $pid = null)
    {
        if ($type == 'p') {
            $rs = $this->db->cacheGetAll("SELECT Pid, Pname, suoxie FROM $this->city_table WHERE province = 1");
        } else {
            $rs = $this->db->cacheGetAll("SELECT cityid, cityname, suoxie FROM $this->city_table WHERE pid = $pid ORDER BY province DESC, abcd ASC");
        }
        return $rs;
    }

    /**
     * del
     * @param int $class_id
     */
    public function delArticle($id)
    {
        $rs = $this->db->execute("DELETE FROM $this->article_table WHERE aid in ($id)");
        return $rs;
    }

}